package com.mustaphakoumach.tusbd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.SimpleCursorAdapter;

public class CalcItinerary {
	private String stopStart;
	private String stopFinal;
//	private String lineStopStart;
//	private String lineStopFinish;
	DataBaseHelper dbHelper;
	SQLiteDatabase db;
	Itinerary itinirary;
	List<HashMap<String, String>> fillMaps;

//	public CalcItinerary(String stopStart, String stopFinal, DataBaseHelper dbHelper2, SQLiteDatabase db2) {
//		super();
////		this.stopStart = stopStart;
////		this.stopFinal = stopFinal;
//		this.dbHelper = dbHelper2;
//		this.db = db2;
//	}

	public CalcItinerary(Itinerary itineraryTuSBD, DataBaseHelper dbHelper2, SQLiteDatabase db2) {
		this.itinirary = itineraryTuSBD;
		this.dbHelper = dbHelper2;
		this.db = db2;
	}

	// /

	public List<HashMap<String, String>> getHorarios1() {
		fillMaps = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < 10; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("rowid", "" + i);
			map.put("col_1", "col_1_item_" + i);
			map.put("col_2", "col_2_item_" + i);
			map.put("col_3", "col_3_item_" + i);
			fillMaps.add(map);
		}
		return fillMaps;
	}

	// /
	public List<HashMap<String, String>> getHorarios() {
		fillMaps = new ArrayList<HashMap<String, String>>();
		String sqlQuery = "SELECT * FROM itinerary WHERE line like '1'";
		if (db != null) {
			Cursor c = db.rawQuery(sqlQuery, null);
			try {
				Log.d("numero lineas", "="+c.getCount());
				if (c.getCount() > 0) {
					c.moveToFirst();
					int i = 0;
					do {
						HashMap<String, String> map = new HashMap<String, String>();
//						map.put("rowid", "" + i);
//						map.put("col_1", "col_1_item_" + i);
//						map.put("col_2", "col_2_item_" + i);
//						map.put("col_3", "col_3_item_" + i);
						map.put("rowid", c.getString(2));
						map.put("col_1", c.getString(1));
						map.put("col_2", c.getString(0));
						map.put("col_3", c.getString(3));
						fillMaps.add(map);
						i++;
					} while (c.moveToNext());
				} else {
					return null;
				}
			} catch (Exception e) {
				return null;// "Error al acceder a las bases de datos.";
			}
			c.close();
			db.close();
		}
		return fillMaps;
	}

	public String SameLine() {
		String SameLine = null;
		ArrayList<String> listLinesStart = getLineStop(stopStart);
		ArrayList<String> listLinesFinish = getLineStop(stopFinal);
		Iterator it1 = listLinesStart.iterator();
		Iterator it2 = listLinesFinish.iterator();
		while (it1.hasNext()) {
			while (it2.hasNext()) {
				if (it1.next() == it2.next()) {
					SameLine = it1.next().toString();
					Log.d("SameLine", SameLine);
				}
			}
		}
		return SameLine;
	}

	public ArrayList<String> getLineStop(String stop) {
		String[] vector = new String[4];
		ArrayList<String> listLines = new ArrayList<String>();
		String sqlQuery = "SELECT * FROM itinerary WHERE line like '1'";
		if (db != null) {
			Cursor c = db.rawQuery(sqlQuery, null);
			try {
				if (c.getCount() > 0) {
					c.moveToFirst();
					do {
						vector[0] = c.getString(0);
						vector[1] = c.getString(1);
						vector[2] = c.getString(2);
						vector[3] = c.getString(3);
						Collections.addAll(listLines, vector);// listLines.add(c.getString(2));
						// Log.d("time start", c.getString(2));
					} while (c.moveToNext());
				} else {
					return null;
				}
			} catch (Exception e) {
				return null;// "Error al acceder a las bases de datos.";
			}
			c.close();
			db.close();
		}
		return listLines;
	}

	public int getDurationItinerary() {
		return 0;

	}

	private boolean getDirectionLine() {
		return false;

	}

}
